package com.supcalls.restful.handler;

import com.supcalls.exception.GlobalException;
import com.supcalls.restful.JsonResult;
import com.supcalls.restful.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * 全局异常处理类
 * @author shrek
 * @version 1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理GlobalException
     */
    @ExceptionHandler
    public JsonResult<Void> handleGlobalException(GlobalException e, HttpServletResponse response) {
        printMessage(e);

        response.setStatus(e.getResponseCode().getValue());

        return JsonResult.error(e.getResponseCode(), e);
    }

    /**
     * 处理AuthenticationException
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public JsonResult<Void> handleAuthenticationException(AuthenticationException e) {
        printMessage(e);

        return JsonResult.error(ResponseCode.FORBIDDEN, e);
    }

    /**
     * 处理BindException
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public JsonResult<Void> handleBindException(BindException e) {
        printMessage(e);

        BindingResult bindingResult = e.getBindingResult();
        FieldError fieldError = bindingResult.getFieldError();
        String defaultMessage = Objects.requireNonNull(fieldError).getDefaultMessage();

        return JsonResult.error(ResponseCode.BAD_REQUEST, defaultMessage);
    }

    /**
     * 处理MethodArgumentNotValidException
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public JsonResult<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        printMessage(e);

        BindingResult bindingResult = e.getBindingResult();
        FieldError fieldError = bindingResult.getFieldError();
        String defaultMessage = Objects.requireNonNull(fieldError).getDefaultMessage();

        return JsonResult.error(ResponseCode.BAD_REQUEST, defaultMessage);
    }

    /**
     * 处理NullPointerException
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult<Void> handleNullPointerException(NullPointerException e) {
        printMessage(e);

        return JsonResult.error(ResponseCode.ERROR, "不规范的代码/请求导致了空指针异常NPE");
    }

    /**
     * 处理Exception
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult<Void> handleException(Exception e) {
        printMessage(e);

        return JsonResult.error(ResponseCode.ERROR, e);
    }

    /**
     * 打印异常信息
     * @param e Exception
     */
    private void printMessage(Exception e) {
        String message = e.getMessage();

        log.error(message);

        e.printStackTrace();
    }

}